package app.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import app.domain.VerificationImage;
import app.vo.VerificationCode;
import app.utils.VerifyImageUtil;
import app.utils.RedisTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
//处理跨域请求
@CrossOrigin
@RequestMapping(value = "/Verification")
public class VerificationController {
    //获取本地存放图片的位置
    @Value("${captcha.slide-verification-code.path.origin-image}")
    private String verificationImagePathPrefix;
    /**
     * redis工具类
     */
    @Autowired
    private RedisTemplateUtils redisTemplateUtils;
    @Autowired
    private VerificationCode pageData;
    @Resource
    private HttpServletRequest request;

    //获取滑动验证码的图片
    @RequestMapping(value = "Image", method = RequestMethod.POST)
    public VerificationCode getImgSwipe() {
        try {
            int index;
            //图片滑动状态
            redisTemplateUtils.set("imgSwipeSuccess", "false");
            //获取图片位置
            File verifyImageImport = new File(verificationImagePathPrefix);
            //获取文件列表
            File[] verifyImages = verifyImageImport.listFiles();
            //随机数 获取当前时间
            Random random = new Random(System.currentTimeMillis());
            //随机取得原图文件夹中一张图片
            assert verifyImages != null;
            index = random.nextInt(verifyImages.length);
            //生成验证图片
            VerificationImage img = VerifyImageUtil.getVerifyImage(verifyImages[index].getPath());
            //向map中存入键值对
            pageData.put("SrcImage", img.getSrcImage());
            pageData.put("CutImage", img.getCutImage());
            pageData.put("YPosition", img.getYPosition());
            pageData.put("SrcImageWidth", img.getSrcImageWidth());
            pageData.put("SrcImageHeight", img.getSrcImageHeight());
            //redis存储x轴位置信息
            redisTemplateUtils.set("XPosition", img.getXPosition(), 2000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回整个对象信息
        return pageData;
    }

    //返回前端滑动验证码参数
    @RequestMapping(value = "VerifyImage", method = RequestMethod.POST)
    public VerificationCode rstImgSwipe() {
        //获取前端发送的x,y轴信息
        pageData = new VerificationCode(request);
        Float xs = Float.valueOf(pageData.getString("x"));
        Float ys = Float.valueOf(pageData.getString("y"));
        //将x轴位置信息转为integer类型
        Integer XPosition = Integer.parseInt(String.valueOf(redisTemplateUtils.get("XPosition")));
        //x轴位置信息乘以y轴位置信息
        Float xResult = XPosition * ys;
        //判断x,y的总值减去x轴信息小于10像素时成功验证否则失败
        if (Math.abs(xResult - xs) < 10) {
            //设置误差像素
            pageData.put("success", true);
            redisTemplateUtils.set("imgSwipeSuccess", "ture");
            return pageData;
        } else {
            pageData.put("success", false);
            redisTemplateUtils.set("imgSwipeSuccess", "false");
            return pageData;
        }
    }
}